package blackpai.backend.service;

import blackpai.backend.dao.ManagerMapper;
import blackpai.backend.entity.Manager;
import blackpai.backend.entity.ManagerExample;
import blackpai.backend.util.MD5Util;
import blackpai.backend.vo.Result;
import blackpai.backend.vo.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service("managerService")
@Transactional
public class ManagerServiceImpl implements ManagerService {

    @Resource
    private ManagerMapper managerMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public List<Manager> findAllManagers() {
        ManagerExample managerExample = new ManagerExample();
        return managerMapper.selectByExample(managerExample);
    }

    @Override
    public Result saveManager(Manager manager) {
        Result result = null;
        try {
            String salt = MD5Util.getSalt(6);
            manager.setSalt(salt);
            manager.setPassword(MD5Util.getMD5((salt+salt)));
            manager.setCreatedate(new Date());
            //manager.setCreateperson(managerCreate.getName());
            managerMapper.insert(manager);
            result = new Result();
            result.setStatus(0);
            result.setData(manager);
            result.setMessags("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result();
            result.setStatus(1);
            result.setData(null);
            result.setMessags("添加失败");
        }
        return result;
    }

    @Override
    public Token managerLogin(String username, String password) {
        ManagerExample managerExample = new ManagerExample();
        Token token = new Token();
        String tokens = MD5Util.getSalt(32);
        managerExample.createCriteria().andUsernameEqualTo(username);
        List<Manager> managers = managerMapper.selectByExample(managerExample);
        for (Manager manager : managers) {
            token.setToken(tokens);
            request.setAttribute("token", tokens);
            restTemplate.getForObject("http://redis-server/redis/setToken?tokens="+tokens+"&managerId="+manager.getManagerid(),String.class );
        }
        return token;
    }

    @Override
    public Result updateManager(Manager manager) {
        Result result = null;
        try{
            Manager byPrimaryKey = managerMapper.selectByPrimaryKey(manager.getManagerid());
            manager.setPassword(MD5Util.getMD5((byPrimaryKey.getSalt()+manager.getPassword())));
            managerMapper.updateByPrimaryKeySelective(manager);
            result = new Result();
            result.setStatus(0);
            result.setData(byPrimaryKey);
            result.setMessags("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setStatus(1);
            result.setMessags("更新失败");
        }
        return  result;
    }

}
